/*
 * @Author: herw
 * @Date: 2021-03-30 10:06:56
 * @Last Modified by: herw
 * @Last Modified time: 2021-04-15 10:07:03
 */
<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialog"
    title="Topic Task"
    width="800px"
  >
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      :rules="rules"
      size="small"
      label-width="116px"
    >
      <el-form-item label="Topic Name" prop="name">
        <el-input v-model="form.name" style="width: 370px" />
      </el-form-item>
      <el-form-item label="Team Name" prop="roleId">
        <el-select
          v-model="form.roleId"
          style="width: 370px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in roles"
            :key="item.name"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Kafka Cluster" prop="clusterIds">
        <el-select
          v-model="form.clusterIds"
          style="width: 370px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in clusters"
            :key="item.name"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Location" prop="location">
        <el-select
          v-model="form.location"
          style="width: 370px"
          placeholder="请选择数据范围"
        >
          <el-option
            v-for="item in locations"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Message Rate" prop="messageRate">
        <el-input-number v-model.number="form.messageRate" :min="1" controls-position="right" style="width: 145px;" /> (message/s)
      </el-form-item>
      <el-form-item label="Partitions" prop="partition">
        <el-input-number v-model.number="form.partition" :min="1" controls-position="right" style="width: 145px;" />
      </el-form-item>
      <el-form-item label="Replication Factor" prop="replication">
        <el-input-number v-model.number="form.replication" :min="1" controls-position="right" style="width: 145px;" />
      </el-form-item>
      <el-form-item label="TTL" prop="ttl">
        <el-input-number v-model.number="form.ttl" :min="1" controls-position="right" style="width: 145px;" /> (H)
      </el-form-item>
      <el-form-item label="描述信息" prop="comments">
        <el-input v-model="form.comments" style="width: 370px;" rows="5" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button
        v-permission="['admin', 'topic:edit']"
        :loading="submitLoading"
        type="primary"
        @click="doApprove"
      >同意</el-button>
      <el-button
        v-permission="['admin', 'topic:edit']"
        :loading="submitLoading"
        type="primary"
        @click="doReject"
      >驳回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import crud from '@/mixins/crud'
import { getAll } from '@/api/system/role'
import { getAllCluster } from '@/api/mq/kafka/cluster'
import { approve, reject } from '@/api/mq/kafka/topicTask'

export default {
  mixins: [crud],
  props: {
    topic: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      dialog: false,
      selectIndex: '',
      locations: ['dev', 'prod'],
      roles: [],
      clusters: [],
      form: this.topic,
      rules: {

      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    // 新增与编辑前做的操作
    beforeInit() {
      this.getRoles()
      this.getClusters()
    },
    // 获取弹窗内角色数据
    getRoles() {
      getAll().then(res => {
        this.roles = res
      }).catch(() => { })
    },
    // 获取弹窗内kafka集群数据
    getClusters() {
      getAllCluster().then(res => {
        this.clusters = res
      }).catch(() => { })
    },
    cancel() {
      this.dialog = false
      this.submitLoading = false
    },
    doApprove() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          approve(this.form).then(res => {
            this.$notify({
              title: '审批成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
          }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
          })
        } else {
          return false
        }
      })
    },
    doReject() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          reject(this.form).then(res => {
            this.$notify({
              title: '审批成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
          }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
